#!/bin/bash
#
#   This file is part of poat.
#
#   poat is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   poat is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with poat.  If not, see <http://www.gnu.org/licenses/>.
#

export SCRIPT_LOCATION="/home/gabriel/poat_control/script"
export LANG="en_US"
export TMP="/tmp"
export PODIR="$TMP/pofiles"
export PRLIST="projectlist"
export FLIST="filelist"
export TLIST="teamlist"
export POAT="php sgs.php "

cd $SCRIPT_LOCATION
echo "Starting stats update: " `date`

if [ -d $PODIR ]
then
	echo "$PODIR already exists"
else
	echo "Creating directory $PODIR"
	mkdir $PODIR
fi


#OBTENER LISTA DE PROJECTOS
echo "Get Project List"
$POAT prj >& $TMP/$PRLIST

for PJ in $(cat $TMP/$PRLIST)
do
	echo "Get Team List For $PJ"
	$POAT team $PJ >& $TMP/$TLIST
	for TI in $(cat $TMP/$TLIST)
	do
	#OBTENER LISTA DE ARCHIVOS
		TID=`echo $TI | cut -f1 -d';'`
		TSID=`echo $TI | cut -f2 -d';'`
		TURL=`echo $TI | cut -f3 -d';'`
		TPATH=`echo $TI | cut -f4 -d';'`
		if [ -d $PODIR$TPATH ]
		then
			echo "Update $TPATH"
			svn up $PODIR$TPATH
		else
			echo "Create $TPATH"
			svn co $TURL $PODIR$TPATH
		fi
		echo "Get File List For $PJ Team $TID"
		$POAT file $PJ $TID $TSID >& $TMP/$FLIST
		for PFI in $(cat $TMP/$FLIST)
		do
			FID=`echo $PFI | cut -f1 -d';'`			
			POFILE=`echo $PFI | cut -f2 -d';'`
			POFILE=`echo $PODIR$TPATH/$POFILE`
			echo "Processing $POFILE"
			if `msgfmt --check-header $POFILE >& /dev/null`
			then
				msgfmt --statistics $POFILE >& $TMP/filestats.txt
				POTRANS=`cat  $TMP/filestats.txt| gawk -F, '{print $1}' | sed 's/ translated messages//'` 
			
				if `echo $POTRANS | grep \. >/dev/null`
				then
					POTRANS=`echo $POTRANS| sed 's/\.//'`
				fi
				if `echo $POTRANS | grep "translated message" >/dev/null`
				then
						POTRANS=`echo $POTRANS| sed 's/ translated message//'`
				fi
				if [ "$POTRANS" == "" ]
				then
					POTRANS="0"
				fi
			
				#in stats are fuzzy skipped, if they are 0 
				if `cat $TMP/filestats.txt | grep fuzzy >/dev/null`
				then
					POFUZZY=`cat  $TMP/filestats.txt|  gawk -F, '{print $2}' | sed 's/ fuzzy translations//' | sed 's/ //'`
					if `echo $POFUZZY | grep "fuzzy translation" >/dev/null`
					then
						POFUZZY=`echo $POFUZZY| sed 's/ fuzzy translation//'`
					fi
					if `echo $POFUZZY | grep \. >/dev/null`
					then
						POFUZZY=`echo $POFUZZY| sed 's/\.//'`
					fi
					if [ "$POFUZZY" == "" ]
					then
							POFUZZY="0"
					fi
					
					POUN=`cat  $TMP/filestats.txt|  gawk -F, '{print $3}' | sed 's/ untranslated messages\.//' | sed 's/ //'`
					if `echo $POUN | grep "untranslated message" >/dev/null`
					then
						POUN=`echo $POUN| sed 's/ untranslated message.//'`
					fi
					if [ "$POUN" == "" ]
					then
						POUN="0"
					fi
				else
						POFUZZY="0"
						POUN=`cat  $TMP/filestats.txt|  gawk -F, '{print $2}' | sed 's/ untranslated messages\.//' | sed 's/ //'`
						if `echo $POUN | grep "untranslated message" >/dev/null`
						then
							POUN=`echo $POUN| sed 's/ untranslated message.//'`
						fi
						if [ "$POUN" == "" ]
						then
							POUN="0"
						fi
				fi
				echo "Save File $POFILE"
				$POAT put $PJ $FID $POTRANS $POFUZZY $POUN >& $TMP/res
				cat $TMP/res
				echo ""
			fi	
			
		done
	done
done
rm $TMP/res
rm $TMP/$PRLIST
rm $TMP/$FLIST
rm $TMP/$TLIST
rm $TMP/filestats.txt
echo "Done"
echo "End stats update: " `date`
